import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';

void main() => runApp(MaterialApp(
      title: 'Material App',
      home: MyApp(),
    ));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final GlobalKey<ScaffoldState> _scaffoldKey;

  @override
  void initState() {
    super.initState();
    _scaffoldKey = GlobalKey<ScaffoldState>();
  }

  showPicker(BuildContext context) {
    Picker picker = Picker(
        cancelText: '取消',
        confirmText: '确定',
        adapter: PickerDataAdapter<String>(
            pickerData: new JsonDecoder().convert(pickerData)),
        changeToFirst: true,
        textAlign: TextAlign.left,
        columnPadding: const EdgeInsets.all(8.0),
        onConfirm: (Picker picker, List value) {
          print(value.toString());
          print(picker.getSelectedValues());
        });
    picker.show(_scaffoldKey.currentState!);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text('Material App Bar'),
      ),
      body: Center(
        child: Container(
          child: TextButton(
            child: Text('选择'),
            onPressed: () {
              showPicker(context);
            },
          ),
        ),
      ),
    );
  }
}

String pickerData = '''
[
  {
    "a": [
      {
        "a1": [
          1,
          2,
          3,
          4
        ]
      },
      {
        "a2": [
          5,
          6,
          7,
          8
        ]
      },
      {
        "a3": [
          9,
          10,
          11,
          12
        ]
      }
    ]
  },
  {
    "b": [
      {
        "b1": [
          11,
          22,
          33,
          44
        ]
      },
      {
        "b2": [
          55,
          66,
          77,
          88
        ]
      },
      {
        "b3": [
          99,
          1010,
          1111,
          1212
        ]
      }
    ]
  },
  {
    "c": [
      {
        "c1": [
          "a",
          "b",
          "c"
        ]
      },
      {
        "c2": [
          "aa",
          "bb",
          "cc"
        ]
      },
      {
        "c3": [
          "aaa",
          "bbb",
          "ccc"
        ]
      }
    ]
  }
]
''';
